package com.blog.wl2023.controller.user;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.ObjectUtil;
import com.blog.wl2023.entity.User;
import com.blog.wl2023.entity.response.Result;
import com.blog.wl2023.service.user.UserService;
import com.blog.wl2023.utils.note.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author wule
 * @desc 用户相关api
 */
@RestController
@RequestMapping("api/com")
@ResponseResult
public class UserController {

	@Autowired
	private UserService userService;

	/**
	 * 登录
	 *
	 * @param user 用户数据
	 * @return 返回token
	 */
	@GetMapping("userLogin")
	public Result userLogin(User user) {
		return userService.userLogin(user);
	}

	/**
	 * 注册
	 *
	 * @param user 接受到的注册参数
	 * @return Result
	 */
	@GetMapping("userRegister")
	public Result userRegister(User user) {
		return userService.userRegister(user);
	}

	/**
	 * 退出登录
	 *
	 * @param token token唯一身份标识
	 * @return Result
	 */
	@GetMapping("userLogout")
	public Result userLogout(String token) {
		StpUtil.logoutByTokenValue(token);
		return Result.ok();
	}

	/**
	 * 获取验证码
	 *
	 * @param user 接收到的用户对象
	 * @return Result
	 */
	@GetMapping("getCode")
	public Result getCode(User user) {
		//判断用户名是否为空,决定是找回密码还是获取注册验证码
		if (ObjectUtil.equal(user.getUsername(), "")) {
			//为空:找回密码验证码
			return userService.getRecoverPasswordCode(user);
		} else {//不为空:获取注册验证码
			return userService.getRegisterCode(user);
		}
	}

	/**
	 * 更新密码功能
	 * @param user 接收到的参数:邮箱,验证码,新密码
	 * @return Result
	 */
	@GetMapping("recoverPassword")
	public Result recoverPassword(@RequestParam("user") User user) {
		return userService.recoverPassword(user);
	}

	/**
	 * 对登陆状态进行校验
	 * @return Boolean
	 */
	@GetMapping("verifyLogin")
	public Result verifyLogin(){
		return userService.verifyLogin();
	}

}
